package com.ayf.payment.game.api.service.platform.impl;

import com.ayf.payment.game.api.config.SystemInitialization;
import com.ayf.payment.game.api.dto.platform.PaymentChannelDTO;
import com.ayf.payment.game.api.dto.platform.PaymentProductAndRateDTO;
import com.ayf.payment.game.api.dto.platform.PaymentProductDTO;
import com.ayf.payment.game.api.entity.MerchantSettleRateCustom;
import com.ayf.payment.game.api.entity.platform.PaymentProduct;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.mapper.MerchantSettleRateCustomMapper;
import com.ayf.payment.game.api.mapper.platform.PaymentProductMapper;
import com.ayf.payment.game.api.service.platform.PaymentProductService;
import com.ayf.payment.game.api.type.Status;
import com.swwx.charm.commons.lang.utils.BeanConvertUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class PaymentProductServiceImpl implements PaymentProductService {

    @Resource
    private PaymentProductMapper paymentProductMapper;
    @Resource
    private SystemInitialization systemInitialization;
    @Resource
    private MerchantSettleRateCustomMapper merchantSettleRateCustomMapper;
    @Resource
    private com.ayf.payment.game.api.config.SystemInitialization SystemInitialization;

    @Override
    public List<PaymentProductDTO> selectAll() {
        Example example = new Example(PaymentProduct.class);
        example.setOrderByClause("serial_number DESC");
        List<PaymentProduct> list= paymentProductMapper.selectByExample(example);
        return BeanConvertUtils.convert(list, PaymentProductDTO.class);
    }

    @Override
    public List<PaymentProduct> getFirstPaymentProduct() {
        List<String> codeList=paymentProductMapper.getPaymentProductBusinessCodeByGroup();
        List<PaymentProduct> list=new ArrayList<>();
        for(String code:codeList){
            PaymentProduct paymentProduct=paymentProductMapper.getFirstPaymentProductByBusinessCode(code);
            list.add(paymentProduct);
        }
        return list;
    }


    @Override
    public void updateProductStatus(PaymentProductDTO paymentProductDTO) {
        PaymentChannelDTO paymentChannelDTO = paymentProductMapper.getChannelStatusByProductId(paymentProductDTO.getId());
        if (paymentChannelDTO == null) {
            throw new ParameterException("产品id传入错误");
        }
        if (paymentChannelDTO.getChannelFlag() == 0) {
            throw new ParameterException("当前产品所属通道为关闭状态，请先开启通道");
        }
        paymentProductMapper.updateProductStatus(paymentProductDTO.getId(),paymentProductDTO.getProductStatus());

        MerchantSettleRateCustom custom=new MerchantSettleRateCustom();
        custom.setPayProductId(paymentProductDTO.getId());
        if (paymentProductDTO.getProductStatus() == 0) {
            custom.setStatus(Status.PROHIBIT);
        } else {
            custom.setStatus(Status.ENABLE);
        }
        merchantSettleRateCustomMapper.updateStatus(custom);

        systemInitialization.initChannel();
    }

    @Override
    public void updatePaymentProduct(PaymentProductDTO paymentProductDTO) {
        PaymentProduct paymentProduct=BeanConvertUtils.convert(paymentProductDTO, PaymentProduct.class);
        paymentProductMapper.updateByPrimaryKeySelective(paymentProduct);
        SystemInitialization.InitializationData("修改支付产品信息" );
    }

    @Override
    public PaymentProductDTO getPaymentProduct(int paymentProductId) {
        PaymentProduct paymentProduct=paymentProductMapper.selectByPrimaryKey(paymentProductId);
        return BeanConvertUtils.convert(paymentProduct, PaymentProductDTO.class);
    }

    @Override
    public List<PaymentProductDTO> updateSerialNumber(PaymentProductDTO paymentProductDTO) {
        paymentProductMapper.updateSerialNumber(paymentProductDTO);
        return selectAll();
    }

    @Override
    public PaymentProductAndRateDTO getPaymentProductAndRateDTO(int paymentProductId) {
        return paymentProductMapper.getPaymentProductAndRateDTO(paymentProductId);
    }

    @Override
    public PaymentProductAndRateDTO getPaymentMerchantProductAndRateDTO(int merchantId, int paymentProductId) {
        return paymentProductMapper.getPaymentMerchantProductAndRateDTO(merchantId,paymentProductId);
    }

    @Override
    public Integer countProductAndMerchant(int merchantId, String payCode) {
        return paymentProductMapper.countProductAndMerchant(merchantId,payCode);
    }
}
